<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRole } from '@/api/settings'
import { getuserByid, assignRoles } from '@/api/user'
export default {
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 多选框选中数据的id
      list: []// 多选框数据
    }
  },
  created() {
    this.loadgetRole()
  },
  methods: {
    async  hSubmit() {
      try {
        const res = await assignRoles({ id: this.userId, roleIds: this.roleIds })
        console.log(res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取数据
    async loadgetRole() {
      const res = await getRole({ page: 1, pagesize: 100 })
      const roleIds = await getuserByid(this.userId)
      console.log(roleIds, '11111111111111111111111')
      this.roleIds = roleIds.data.roleIds
      console.log(res)
      // 绑定数据
      this.list = res.data.rows
    },
    closeDialog() {

    }
  }
}
</script>
